import os
from flask_cors import CORS


class Config:
    """Config app全局配置类"""
    # 获取项目根目录
    ROOT_DIR = os.path.dirname(__file__)
    # SECRET_KEY密钥配置
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'PmCoder'
    # Mysql数据库配置，在不需要跟踪对象变化时降低内存消耗
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    # 用户默认密码
    DEFAULT_PWD = '123456'

    # 定义了一个初始化app的静态方法
    @staticmethod
    def init_app(app):
        pass
        # 禁用跨域保护
        cors = CORS(app, resources={r'/*': {'origins': '*'}})


class DevConfig(Config):
    """开发环境配置"""
    DEBUG = True
    # DEFAULT_PWD = '123456'

    # 数据库参数配置
    HOST = '192.168.5.110'
    PORT = '3306'
    DATABASE = 'data_analysis'
    USERNAME = 'data_analysis'
    PASSWORD = 'kF2eLZLCB5L4hACm'

    SQLALCHEMY_DATABASE_URI = \
        "mysql://{}:{}@{}:{}/{}?charset=utf8".format(USERNAME, PASSWORD, HOST, PORT, DATABASE)


class ProConfig(Config):
    """生产环境配置"""

    # 生产环境数据库配置
    HOST = os.environ.get('HOST')
    PORT = os.environ.get('PORT')
    DATABASE = os.environ.get('DATABASE')
    USERNAME = os.environ.get('USERNAME')
    PASSWORD = os.environ.get('PASSWORD')

    SQLALCHEMY_DATABASE_URI = \
        "mysql://{}:{}@{}:{}/{}?charset=utf8".format(USERNAME, PASSWORD, HOST, PORT, DATABASE)

    # 启用缓慢查询记录功能
    SQLALCHEMY_RECORD_QUERIES = True
    FBADMIN_SLOW_DB_QUERY_TIME = 0.5


# 通过config导入环境配置
config = {
    'dev': DevConfig,
    'pro': ProConfig
}
